package top.jojoace.RequestParam_DTO_Validate.Controller;

import org.springframework.web.bind.annotation.*;
import top.jojoace.RequestParam_DTO_Validate.dto.PersonDTO;
import top.jojoace.RequestParam_DTO_Validate.dto.PersonDTO2;

import java.util.Map;

@RestController
@RequestMapping("")
public class UriParam_DTO_Controller {


    /**
     * RestFul风格
     * <p>
     * <p>
     * Request: /{id}
     *
     * @return
     * @PathVariable(name = "id") Integer id
     * name可省略
     * <p>
     * Request: /XX?N1=XX,N2=XXX
     * @RequestParam(name = "N1") String name
     * name可省略
     */

    // request : /test2  -> name=user1,number=1
    // request : /test2?name="NAME"  -> name=NAME,number=1
    // request : /test2?number=1 -> name=user1,number=1,
    @GetMapping("/test2")
    public Object get(@RequestParam(name = "name",defaultValue = "user1") String name, @RequestParam(defaultValue = "1") Integer number) {
        return name + "_" + number;
    }



    //restFull
    @GetMapping("/test/{id}")
    public Object test(@PathVariable Integer id) {
        return id;
    }


    //restFull + requestBody
    // request : /test2/11?name="NAME"
    @GetMapping("/test2/{id2}")
    public Object test2(@PathVariable(name = "id2") Integer id, @RequestParam String name) {
        return id + "_" + name;
    }


    //requestBody
    // request_Body : {"id": 12, "name": "NA"}
    @GetMapping("/test3")
    public Object test3(@RequestBody Map<String, Object> map) {
        return (String) map.get("name") + "_" + (Integer) map.get("id");
    }

    //DTO传输
    // request_Body : {"id": 12, "name": "NA"}
    @GetMapping("/test4")
    public Object test4(@RequestBody PersonDTO person) {
        return person.getId() + "_" + person.getName();
    }


    //DTO builder
    @GetMapping("/test5")
    public Object test5(@RequestBody PersonDTO2 person) {
        PersonDTO2 dto2 = PersonDTO2.builder()
                .name("NA")
                .id(12)
                .build();
        return person.getId() + "_" + person.getName();
    }

}
